package ookweb;

import java.io.BufferedReader;
import java.io.PrintStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import oracle.sql.CLOB;

public class Str {
	public String toChinese(String str) {
		if ((str == null) || (str.length() < 1))
			str = "";
		else {
			try {
				str = new String(str.getBytes("iso-8859-1"), "utf-8");
			} catch (UnsupportedEncodingException e) {
				System.err.print(e.getMessage());
				e.printStackTrace();
				return str;
			}
		}
		return str;
	}

	public String dbEncode(String str) {
		if (str == null)
			str = "";
		else {
			try {
				str = str.replace('\'', '\001').trim();
			} catch (Exception e) {
				System.err.print(e.getMessage());
				e.printStackTrace();
				return str;
			}
		}
		return str;
	}

	public String filterHtml(String value) {
		value = value.replaceAll("<", "&lt;");
		value = value.replaceAll(">", "&gt;");
		value = value.replaceAll(" ", "&nbsp;");
		value = value.replaceAll("'", "&#39;");
		value = value.replaceAll("\"", "&quot;");
		value = value.replaceAll("\n", "<br>");
		value = value.replaceAll("&lt;br&gt;", "<br>");
		value = value.replaceAll("&lt;p&gt;", "<p>");
		value = value.replaceAll("&lt;BR&gt;", "<br>");
		value = value.replaceAll("&lt;BR&gt;", "<br>");
		value = value.replaceAll("&lt;P&gt;", "<P>");
		value = value.replaceAll("&lt;/p&gt;", "</p>");
		value = value.replaceAll("&lt;/P&gt;", "</P>");
		return value;
	}

	public String filterHtml2(String value) {
		value = value.replaceAll("&lt;", "<");
		value = value.replaceAll("&gt;", ">");

		value = value.replaceAll("&#39;", "'");
		value = value.replaceAll("&quot;", "\"");

		value = value.replaceAll("&lt;br&gt;", "<br>");
		value = value.replaceAll("&lt;p&gt;", "<p>");
		value = value.replaceAll("&lt;BR&gt;", "<br>");
		value = value.replaceAll("&lt;BR&gt;", "<br>");
		value = value.replaceAll("&lt;P&gt;", "<P>");
		value = value.replaceAll("&lt;/p&gt;", "</p>");
		value = value.replaceAll("&lt;/P&gt;", "</P>");
		return value;
	}

	public String filterHtmlrc(String value) {
		value = value.replaceAll(" ", "&nbsp;");
		value = value.replaceAll("'", "&#39;");
		value = value.replaceAll("\n", "<br>");
		value = value.replaceAll("\"", "&quot;");
		return value;
	}

	public static boolean sql_inj(String str) {
		String inj_str = "'|and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare|;|or|-|+|,";
		String[] inj_stra = inj_str.split("\\|");
		for (int i = 0; i < inj_stra.length; i++) {
			if (str.indexOf(inj_stra[i]) >= 0) {
				return true;
			}
		}
		return false;
	}

	public static String parse(CLOB clob) {
		String content = "";
		try {
			Reader is = clob.getCharacterStream();
			BufferedReader br = new BufferedReader(is);
			String s = null;
			while ((s = br.readLine()) != null)
				content = content + s;
		} catch (Exception localException) {
		}
		return content;
	}
}
